package com.exam.master.model.main.view;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.exam.commonbiz.base.BaseFragment;
import com.exam.commonbiz.bean.MessageInfo;
import com.exam.master.R;
import com.exam.master.WXApplication;
import com.exam.master.databinding.FragmentHomeMessageBinding;
import com.exam.commonbiz.model.IHomeMessageCallBack;
import com.exam.commonbiz.viewmodel.CommonViewModel;
import com.exam.master.model.main.adapter.HomeMessageAdapter;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeMessageFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeMessageFragment extends BaseFragment<CommonViewModel, FragmentHomeMessageBinding> implements IHomeMessageCallBack, BaseQuickAdapter.RequestLoadMoreListener {

    private String mMasterId;
    private HomeMessageAdapter mHomeMessageAdapter;

    public static HomeMessageFragment newInstance() {
        HomeMessageFragment fragment = new HomeMessageFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home_message;
    }

    @Override
    public void initData() {

        mHomeMessageAdapter = new HomeMessageAdapter();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        binding.recyclerView.setLayoutManager(linearLayoutManager);
        binding.recyclerView.setAdapter(mHomeMessageAdapter);
        mHomeMessageAdapter.setOnItemClickListener(new HomeMessageAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(MessageInfo messageInfo) {
                MessageDetailActivity.start(context, messageInfo.nickname, messageInfo.id);
            }
        });
        binding.recyclerView.setNestedScrollingEnabled(true);
        mHomeMessageAdapter.setOnLoadMoreListener(this, binding.recyclerView);

        binding.swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData();
            }
        });

        mViewModel.setIHomeMessageCallBack(this);
        mHomeMessageAdapter.disableLoadMoreIfNotFullPage();
        binding.swipeRefreshLayout.setColorSchemeResources(R.color.main_color);
        binding.recyclerView.setNestedScrollingEnabled(true);
        mMasterId = WXApplication.getInstance().getMasterId();
        getData();
    }

    private void getData() {

        if (mViewModel != null) {
            mViewModel.getMessageMenuList(mMasterId);
        }
    }

    @Override
    protected void loadData() {
        getData();
    }

    @Override
    public void onLoadMoreRequested() {
        mHomeMessageAdapter.loadMoreEnd();
    }

    @Override
    public void returnMessageMenList(List<MessageInfo> messageInfoList) {
        binding.swipeRefreshLayout.setRefreshing(false);
        if (messageInfoList == null || messageInfoList.size() == 0) {
            mHomeMessageAdapter.loadMoreEnd();
            return;
        }
        mHomeMessageAdapter.setNewData(messageInfoList);
        mHomeMessageAdapter.loadMoreComplete();
    }
}